﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SmartEP.Model
{
    public static class DBConnection
    {
        private static string _connectionString;
        private static SqlConnection _connection;

        public static string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_connectionString))
                {
                    _connectionString = @"Data Source=(local);Initial Catalog=sgittest;User ID=sa;Password=1234";
                }

                return _connectionString;
            }
        }

        public static SqlConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    _connection = new SqlConnection(ConnectionString);
                    _connection.Open();
                }
                return _connection;
            }
        }
        public static bool IsConnectDatabase(string ServerName, string DatabaseName, string UserName, string Password)
        {
            try
            {
                string connectionString = Conncect(ServerName, DatabaseName, UserName, Password);
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                _connectionString = connectionString;
                _connection = con;
                return true;
            }
            catch { return false; }
        }
        public static string Conncect(string ServerName, string DatabaseName, string UserName, string Password)
        {
            string StringCon = @"Password = " + Password;
            StringCon += ";User ID = " + UserName;
            StringCon += ";Initial Catalog = " + DatabaseName;
            StringCon += ";Data Source = " + ServerName;
            return StringCon;
        }
    }
}
